#include <unistd.h>

#include <memory>
#include <functional>

#include "tcp_server.hpp"
#include "task.hpp"

#include "log.hpp"
Log log;

void* device_handler(void* args)
{
    constexpr uint16_t port = 8082;
    pthread_setname_np(pthread_self(), "device_thread");
    std::unique_ptr<TCP_server> device_server(new TCP_server);
    device_server->init(port);
    device_server->run(device_task);
}

void* http_handler(void* args)
{
    constexpr uint16_t port = 8083;
    pthread_setname_np(pthread_self(), "http_thread");
    std::unique_ptr<TCP_server> http_server(new TCP_server);
    http_server->init(port);
    http_server->run(http_task);
}

int main()
{
#ifdef __RELEASE__
    log.change_method(Log::Class);
    daemon(1, 0);
#endif
    log(Log::Info, "process running, pid: %d", getpid());

    pthread_t device_thread;
    pthread_create(&device_thread, nullptr, device_handler, nullptr);

    pthread_t http_thread;
    pthread_create(&http_thread, nullptr, http_handler, nullptr);

    pthread_join(device_thread, nullptr);
    pthread_join(http_thread, nullptr);

    return 0;
}
